export default function (plop) {
	plop.setHelper("pascalCase", (text) => {
		return text.replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : "")).replace(/^(.)/, (c) => c.toUpperCase())
	})

	plop.setHelper("camelCase", (text) => {
		return text.replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : "")).replace(/^(.)/, (c) => c.toLowerCase())
	})

	plop.setHelper("kebabCase", (text) => {
		return text
			.replace(/([a-z])([A-Z])/g, "$1-$2")
			.replace(/[\s_]+/g, "-")
			.toLowerCase()
	})

	plop.setGenerator("component", {
		description: "Create a new component",
		prompts: [
			{
				type: "input",
				name: "name",
				message: "Component name:",
				validate: (value) => {
					if (!value) return "Component name is required"
					if (!/^[A-Z][a-zA-Z0-9]*$/.test(value)) {
						return "Component name must be PascalCase"
					}
					return true
				},
			},
		],
		actions: [
			{
				type: "add",
				path: "packages/versakit-components/src/{{kebabCase name}}/index.tsx",
				templateFile: "plop/component/index.tsx.hbs",
			},
			{
				type: "add",
				path: "packages/versakit-components/src/{{kebabCase name}}/{{kebabCase name}}.types.ts",
				templateFile: "plop/component/types.ts.hbs",
			},
			{
				type: "add",
				path: "packages/versakit-components/src/{{kebabCase name}}/{{kebabCase name}}.variants.ts",
				templateFile: "plop/component/variants.ts.hbs",
			},
			{
				type: "add",
				path: "packages/versakit-components/src/{{kebabCase name}}/__test__/{{kebabCase name}}.test.tsx",
				templateFile: "plop/component/__test__/test.tsx.hbs",
			},
			{
				type: "add",
				path: "packages/versakit-components/src/{{kebabCase name}}/README.md",
				templateFile: "plop/component/README.md.hbs",
			},
			{
				type: "add",
				path: "apps/storybook/stories/components/{{pascalCase name}}/{{pascalCase name}}.stories.tsx",
				templateFile: "plop/component/stories.tsx.hbs",
			},
			{
				type: "append",
				path: "packages/versakit-components/src/index.ts",
				pattern: /(\/\/ plop-component-export-placeholder)/gi,
				template:
					'export type { {{pascalCase name}}Props } from "./{{kebabCase name}}"\nexport { {{pascalCase name}}, {{camelCase name}}Variants } from "./{{kebabCase name}}"\n// plop-component-export-placeholder',
			},
		],
	})
}
